// number dialogs
#include "burner.h"

// ----------------------------------------------------------------------------
// CPU clock dialog

static int exitStatus = 0;

static INT_PTR CALLBACK cpuClockProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM)	// LPARAM lParam
{
	switch (msg) {
		case WM_INITDIALOG: {
			wchar_t szText[16];
			exitStatus = 0;

			wndInMid(hDlg, hScrnWnd);

			// Initialise slider
			SendDlgItemMessage(hDlg, IDC_CPUCLOCK_SLIDER, TBM_SETRANGE, 0, (LPARAM)MAKELONG(0x80, 0x0500));
			SendDlgItemMessage(hDlg, IDC_CPUCLOCK_SLIDER, TBM_SETLINESIZE, 0, (LPARAM)0x05);
			SendDlgItemMessage(hDlg, IDC_CPUCLOCK_SLIDER, TBM_SETPAGESIZE, 0, (LPARAM)0x10);
			SendDlgItemMessage(hDlg, IDC_CPUCLOCK_SLIDER, TBM_SETTIC, 0, (LPARAM)0x0100);
			SendDlgItemMessage(hDlg, IDC_CPUCLOCK_SLIDER, TBM_SETTIC, 0, (LPARAM)0x0200);
			SendDlgItemMessage(hDlg, IDC_CPUCLOCK_SLIDER, TBM_SETTIC, 0, (LPARAM)0x0300);
			SendDlgItemMessage(hDlg, IDC_CPUCLOCK_SLIDER, TBM_SETTIC, 0, (LPARAM)0x0400);

			// Set slider to current value
			SendDlgItemMessage(hDlg, IDC_CPUCLOCK_SLIDER, TBM_SETPOS, (WPARAM)true, (LPARAM)nBurnCPUSpeedAdjust);

			// Set the edit control to current value
			swprintf(szText, L"%i", nBurnCPUSpeedAdjust * 100 / 256);
			SendDlgItemMessage(hDlg, IDC_CPUCLOCK_EDIT, WM_SETTEXT, 0, (LPARAM)szText);

			return TRUE;
		}
		case WM_COMMAND: {
			switch (HIWORD(wParam)) {
				case BN_CLICKED: {
					if (LOWORD(wParam) == IDOK) {
						exitStatus = 1;
						SendMessage(hDlg, WM_CLOSE, 0, 0);
					}
					else if (LOWORD(wParam) == IDCANCEL) {
						exitStatus = -1;
						SendMessage(hDlg, WM_CLOSE, 0, 0);
					}
					else if (LOWORD(wParam) == ID_DEFAULT) {
						nBurnCPUSpeedAdjust = 0x100;
						SendDlgItemMessage(hDlg, IDC_CPUCLOCK_SLIDER, TBM_SETPOS, (WPARAM)true, (LPARAM)nBurnCPUSpeedAdjust);
						wchar_t szText[16];
						swprintf(szText, L"%i", nBurnCPUSpeedAdjust * 100 / 256);
						SendDlgItemMessage(hDlg, IDC_CPUCLOCK_EDIT, WM_SETTEXT, 0, (LPARAM)szText);
					}
					break;
				}
				case EN_UPDATE: {
					if (exitStatus == 0) {
						wchar_t szText[16] = L"";
						bool bValid = 1;
						int nValue;

						if (SendDlgItemMessage(hDlg, IDC_CPUCLOCK_EDIT, WM_GETTEXTLENGTH, 0, 0) < 16) {
							SendDlgItemMessage(hDlg, IDC_CPUCLOCK_EDIT, WM_GETTEXT, (WPARAM)16, (LPARAM)szText);
						}

						// Scan string in the edit control for illegal characters
						for (int i = 0; szText[i]; i++) {
							if (! iswdigit(szText[i])) {
								bValid = 0;
								break;
							}
						}

						if (bValid) {
							nValue = wcstol(szText, NULL, 0);
							if (nValue < 25) {
								nValue = 25;
							} else { // Extra values (FB Alpha Plus!)
								if (nValue > 500) {
									nValue = 500;
								}
							}

							nValue = (int)((double)nValue * 256.0 / 100.0 + 0.5);

							// Set slider to current value
							SendDlgItemMessage(hDlg, IDC_CPUCLOCK_SLIDER, TBM_SETPOS, (WPARAM)true, (LPARAM)nValue);
						}
					}
					break;
				}
			}
			break;
		}

		case WM_HSCROLL: {
			switch (LOWORD(wParam)) {
				case TB_BOTTOM:
				case TB_ENDTRACK:
				case TB_LINEDOWN:
				case TB_LINEUP:
				case TB_PAGEDOWN:
				case TB_PAGEUP:
				case TB_THUMBPOSITION:
				case TB_THUMBTRACK:
				case TB_TOP: {
					if (exitStatus == 0) {
						// Update the contents of the edit control
						int nValue = SendDlgItemMessage(hDlg, IDC_CPUCLOCK_SLIDER, TBM_GETPOS, 0, 0);
						nValue = (int)((double)nValue * 100.0 / 256.0 + 0.5);
						wchar_t szText[16];
						swprintf(szText, L"%i", nValue);
						SendDlgItemMessage(hDlg, IDC_CPUCLOCK_EDIT, WM_SETTEXT, 0, (LPARAM)szText);
					}
					break;
				}
			}
			break;
		}

		case WM_CLOSE:
			if (exitStatus == 1) {
				wchar_t szText[16] = L"";
				SendDlgItemMessage(hDlg, IDC_CPUCLOCK_EDIT, WM_GETTEXT, (WPARAM)16, (LPARAM)szText);
				nBurnCPUSpeedAdjust = wcstol(szText, NULL, 0);
				if (nBurnCPUSpeedAdjust < 25) {
					nBurnCPUSpeedAdjust = 25;
				} else {
					if (nBurnCPUSpeedAdjust > 500) {
						nBurnCPUSpeedAdjust = 500;
					}
				}

				nBurnCPUSpeedAdjust = (int)((double)nBurnCPUSpeedAdjust * 256.0 / 100.0 + 0.5);
			}
			EndDialog(hDlg, 0);
			break;
	}

	return 0;
}

void cpuClockDialog(HWND parent)
{
	localiseDialogBox(IDD_CPUCLOCK, parent, (DLGPROC)cpuClockProc);
}

// ----------------------------------------------------------------------------
// color adjust dialog, added by regret

static void colorAdjustSetValue(HWND hDlg)
{
	wchar_t szText[32];
	_snwprintf(szText, sizearray(szText), localiseStringEx(IDS_COLOR_CONTRAST), color_contrast);
	SetDlgItemText(hDlg, IDC_CONTRAST_TEXT, szText);
	_snwprintf(szText, sizearray(szText), localiseStringEx(IDS_COLOR_BRIGHTNESS), color_brightness);
	SetDlgItemText(hDlg, IDC_BRIGHT_TEXT, szText);
	_snwprintf(szText, sizearray(szText), localiseStringEx(IDS_COLOR_GAMMA), color_gamma);
	SetDlgItemText(hDlg, IDC_GAMMA_TEXT, szText);

	SendDlgItemMessage(hDlg, IDC_CONTRAST_SLIDER, TBM_SETPOS, (WPARAM)TRUE, (LPARAM)color_contrast);
	SendDlgItemMessage(hDlg, IDC_BRIGHT_SLIDER, TBM_SETPOS, (WPARAM)TRUE, (LPARAM)color_brightness);
	SendDlgItemMessage(hDlg, IDC_GAMMA_SLIDER, TBM_SETPOS, (WPARAM)TRUE, (LPARAM)color_gamma);

	CheckDlgButton(hDlg, IDC_GRAYSCALE, color_grayscale ? BST_CHECKED : BST_UNCHECKED);
	CheckDlgButton(hDlg, IDC_INVERT, color_invert ? BST_CHECKED : BST_UNCHECKED);
	CheckDlgButton(hDlg, IDC_SEPIA, color_sepia ? BST_CHECKED : BST_UNCHECKED);
}

static INT_PTR CALLBACK colorAdjustProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM)
{
	switch (msg) {
		case WM_INITDIALOG: {
//			wndInMid(hDlg, hScrnWnd);

			// Initialise slider
			SendDlgItemMessage(hDlg, IDC_CONTRAST_SLIDER, TBM_SETRANGE, (WPARAM)TRUE, (LPARAM)MAKELONG(-95, 95));
			SendDlgItemMessage(hDlg, IDC_CONTRAST_SLIDER, TBM_SETPAGESIZE, 0, (LPARAM)10);
			SendDlgItemMessage(hDlg, IDC_CONTRAST_SLIDER, TBM_SETTIC, 0, 0);
			SendDlgItemMessage(hDlg, IDC_BRIGHT_SLIDER, TBM_SETRANGE, (WPARAM)TRUE, (LPARAM)MAKELONG(-95, 95));
			SendDlgItemMessage(hDlg, IDC_BRIGHT_SLIDER, TBM_SETPAGESIZE, 0, (LPARAM)10);
			SendDlgItemMessage(hDlg, IDC_BRIGHT_SLIDER, TBM_SETTIC, 0, 0);
			SendDlgItemMessage(hDlg, IDC_GAMMA_SLIDER, TBM_SETRANGE, (WPARAM)TRUE, (LPARAM)MAKELONG(-95, 95));
			SendDlgItemMessage(hDlg, IDC_GAMMA_SLIDER, TBM_SETPAGESIZE, 0, (LPARAM)10);
			SendDlgItemMessage(hDlg, IDC_GAMMA_SLIDER, TBM_SETTIC, 0, 0);

			colorAdjustSetValue(hDlg);

			// Update the screen
			if (vidOkay) {
				videoInterface.redraw();
				videoInterface.paint(0);
			}

			return TRUE;
		}

		case WM_COMMAND: {
			switch (HIWORD(wParam)) {
				case BN_CLICKED: {
					bool update = false;

					if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) {
						SendMessage(hDlg, WM_CLOSE, 0, 0);
					}
					else if (LOWORD(wParam) == ID_DEFAULT) {
						color_contrast = color_brightness = color_gamma = 0;
						color_grayscale = color_invert = color_sepia = 0;

						colorAdjustSetValue(hDlg);

						update = true;
					}

					if (LOWORD(wParam) == IDC_GRAYSCALE) {
						color_grayscale = !color_grayscale;
						update = true;
					}
					else if (LOWORD(wParam) == IDC_INVERT) {
						color_invert = !color_invert;
						update = true;
					}
					else if (LOWORD(wParam) == IDC_SEPIA) {
						color_sepia = !color_sepia;
						update = true;
					}

					// Update the screen
					if (update && vidOkay) {
						videoInterface.redraw();
						videoInterface.paint(0);
					}
					break;
				}
				break;
			}
		}

		case WM_HSCROLL: {
			switch (LOWORD(wParam)) {
				case TB_BOTTOM:
				case TB_ENDTRACK:
				case TB_LINEDOWN:
				case TB_LINEUP:
				case TB_PAGEDOWN:
				case TB_PAGEUP:
				case TB_THUMBPOSITION:
				case TB_THUMBTRACK:
				case TB_TOP: {
					wchar_t szText[32];

					color_contrast = SendDlgItemMessage(hDlg, IDC_CONTRAST_SLIDER, TBM_GETPOS, 0, 0);
					_snwprintf(szText, sizearray(szText), localiseStringEx(IDS_COLOR_CONTRAST), color_contrast);
					SetDlgItemText(hDlg, IDC_CONTRAST_TEXT, szText);

					color_brightness = SendDlgItemMessage(hDlg, IDC_BRIGHT_SLIDER, TBM_GETPOS, 0, 0);
					_snwprintf(szText, sizearray(szText), localiseStringEx(IDS_COLOR_BRIGHTNESS), color_brightness);
					SetDlgItemText(hDlg, IDC_BRIGHT_TEXT, szText);

					color_gamma = SendDlgItemMessage(hDlg, IDC_GAMMA_SLIDER, TBM_GETPOS, 0, 0);
					_snwprintf(szText, sizearray(szText), localiseStringEx(IDS_COLOR_GAMMA), color_gamma);
					SetDlgItemText(hDlg, IDC_GAMMA_TEXT, szText);

					// Update the screen
					if (vidOkay) {
						videoInterface.redraw();
						videoInterface.paint(0);
					}
					break;
				}
			}
			break;
		}

		case WM_CLOSE:
			EndDialog(hDlg, 0);
			break;
	}

	return 0;
}

void colorAdjustDialog(HWND parent)
{
	localiseDialogBox(IDD_COLORADJUST, parent, (DLGPROC)colorAdjustProc);
}

// ----------------------------------------------------------------------------
// aspect set dialog, added by regret

static INT_PTR CALLBACK aspectProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch (msg) {
		case WM_INITDIALOG:
//			wndInMid(hDlg, hScrnWnd);

			// Set the edit control to current values
			wchar_t szText[16];
			swprintf(szText, L"%i", vidScrnAspectX);
			SendDlgItemMessage(hDlg, IDC_ASPECTX_EDIT, WM_SETTEXT, 0, (LPARAM)szText);
			swprintf(szText, L"%i", vidScrnAspectY);
			SendDlgItemMessage(hDlg, IDC_ASPECTY_EDIT, WM_SETTEXT, 0, (LPARAM)szText);
			return TRUE;

		case WM_COMMAND:
			switch (HIWORD(wParam)) {
				case BN_CLICKED:
					if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) {
						SendMessage(hDlg, WM_CLOSE, 0, 0);
					}
					break;

				case EN_UPDATE:
					wchar_t szText[16] = L"";
					int aspect = 0;

					switch (LOWORD(wParam)) {
						case IDC_ASPECTX_EDIT:
							SendDlgItemMessage(hDlg, IDC_ASPECTX_EDIT, WM_GETTEXT, (WPARAM)sizearray(szText), (LPARAM)szText);
							aspect = wcstol(szText, NULL, 0);
							if (aspect > 0) {
								vidScrnAspectX = aspect;
							}
							break;
						case IDC_ASPECTY_EDIT:
							SendDlgItemMessage(hDlg, IDC_ASPECTY_EDIT, WM_GETTEXT, (WPARAM)sizearray(szText), (LPARAM)szText);
							aspect = wcstol(szText, NULL, 0);
							if (aspect > 0) {
								vidScrnAspectY = aspect;
							}
							break;
					}
					break;
			}
			break;

		case WM_CLOSE:
			EndDialog(hDlg, 0);
			break;
	}

	return FALSE;
}

void aspectSetDialog(HWND parent)
{
	localiseDialogBox(IDD_ASPECTSET, parent, (DLGPROC)aspectProc);
}
